//
//  AppDelegate.swift
//  weibo
//
//  Created by zcp on 16/5/9.
//  Copyright © 2016年 zcp. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //设置导航栏皮肤
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        //创建widow
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        //设置widow根控制器
//        let tabbar = CPMainTabbarViewController()
        window?.rootViewController = defaultViewController()
//        window?.rootViewController = CPNewFeatrueCollectionViewController()
               //设置widow为主窗口并可见
        window?.makeKeyAndVisible()
        return true
    }
    
    //进入默认的控制器
    private func defaultViewController() -> UIViewController{
    
        //判断是否登录
        if CPUserAccountViewModel.sharedAccountViewModel.isLogin()
        {
            //登录了
            //是否为新版本
            if isNewVersion()
            {
                //是新版本
                return CPNewFeatrueCollectionViewController()
            
            }
            else
            {
                //不是新版本
                return CPWelcomeViewController()
            }
        }
        else//没有登录
        {
            return CPMainTabbarViewController()
        }
        
    
    }
    //外界调用时,把跟控制器切换为外界传入的控制器
    func switchRootViewController(controller: UIViewController){
    
        window?.rootViewController = controller
    }

    //判断是否为新版本
    func isNewVersion() -> Bool{
        //获取当前版本
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as? String
        
        
        //拿到沙盒保存的版本进行比较
        let sanheKey = "sanheKey"
        let sanheVersion = NSUserDefaults.standardUserDefaults().stringForKey(sanheKey)
        //不是新版本,返回false
        if sanheVersion == currentVersion
        {
        
            return false
        }
        //是新版本,保存新版本到沙盒,并返回true
        NSUserDefaults.standardUserDefaults().setObject(currentVersion, forKey: sanheKey)
        NSUserDefaults.standardUserDefaults().synchronize()

    return true
    }
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

